
package com.hwadee.JPKC.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hwadee.JPKC.entity.courseDeclare;
import com.hwadee.JPKC.entity.courseInfo;
import com.hwadee.JPKC.entity.user;
import com.hwadee.JPKC.service.CourseDeclareService;
import com.hwadee.JPKC.service.CourseInfoService;

/**

* @ClassName: Test

* @Description: TODO(这里用一句话描述这个类的作用)

* @author hui

* @date 2020-03-03

* @Copyright ? 四川华迪信息有限公司

*/
@Controller
@RequestMapping("/admin/declare")
public class CourseDeclareController {
	
	@Autowired
	private CourseDeclareService courseDeclareService;
	
	@Autowired
	private CourseInfoService courseInfoService;
	
	@Autowired
	private ServletContext application;
	
	/**
	 * 方法：profile 课程详情
	 * @param p
	 * @param model
	 * @return String
	 * @date 2020-03-07
	 */
	@RequestMapping("/profile.ht")
	public String profile(@RequestParam(name = "p", required = true, defaultValue = "1") Integer p,Integer courseId,Model model) {
		
		Page<courseDeclare> coursedeclare = PageHelper.startPage(p, 3).doSelectPage(()->courseDeclareService.FindPage());
		model.addAttribute("coursedeclare", coursedeclare);
	
		courseInfo courseid = courseInfoService.selectByPrimaryKey(courseId);
		model.addAttribute("courseid", courseid);
		return "admin/profile";
	}
	
	/**
	 * 方法：FindAll 课程申报审核
	 * @param p
	 * @param model
	 * @return String
	 * @date 2020-03-07
	 */
	@RequestMapping("/FindAll.ht")
	public String FindAll(@RequestParam(name = "p", required = true, defaultValue = "1") Integer p,Model model) {
		
<<<<<<< HEAD
		Page<courseDeclare> coursedeclare = PageHelper.startPage(p, 1).doSelectPage(()->courseDeclareService.FindPage());
	
=======
		Page<courseDeclare> coursedeclare = PageHelper.startPage(p, 5).doSelectPage(()->courseDeclareService.FindPage());
>>>>>>> e92cca4bb06cf37fd9e459ee7ced6af37b6175bf
		model.addAttribute("coursedeclare", coursedeclare);
	
		return "admin/basic-table";
		
	}
	
	/*
	 * 
	 */
	@RequestMapping("/shanchu.ht")
	public String sancu(Integer serialNumber) {
		
		courseDeclareService.deleteByPrimaryKey(serialNumber);
		
		return "redirect:/admin/FindAll.ht";
		
	}
	
	
	
	/**
	 * 方法：更改课程状态
	 * @param coursedeclare
	 * @return String
	 * @date 2020-03-08
	 */
	@RequestMapping("/Sh")
	public String SH(courseDeclare coursedeclare,courseInfo courseInfo) {

		coursedeclare.setTechnical("已通过");
		courseDeclareService.updateByPrimaryKeySelective(coursedeclare);
		System.out.println(coursedeclare.getcName());
		courseInfo = courseInfoService.SelectName(coursedeclare.getcName());
		courseInfo.setAcademicExchange("已上线");
		courseInfoService.updateByPrimaryKeySelectiveName(courseInfo);	

		return "redirect:/admin/declare/FindAll.ht";
	}
	
	/**
	 * 方法：跳到课程申请页
	 * @return String
	 * @date 2020-03-16
	 */
	@RequestMapping("/gl.do")
	public String gl() {
		return "gl";
	}
	/**
	 * 方法：跳到课程申请页
	 * @return String
	 * @date 2020-03-16
	 */
	@RequestMapping("/ht.do")
	public String ht() {
		return "ht";
	}
	/**
	 * 方法：申请课程
	 * @param coursedeclare
	 * @return String
	 * @date 2020-03-09
	 */
	@PostMapping("/insertCourse")
	public String insertCourse(MultipartFile file,courseDeclare coursedeclare,courseInfo courseinfo,HttpSession session,Model model)throws IOException {
		user u  = (user)session.getAttribute("u");
		coursedeclare.setTechnical("待定");
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		coursedeclare.setTime(formatter.format(calendar.getTime()));
		coursedeclare.setUserId(u.getUserId());
		courseDeclareService.insertKc(coursedeclare);
		String path = application.getRealPath("/upload");
		// 文件存储路径
		String ofn = file.getOriginalFilename();
		System.out.println(ofn);
		System.out.println(path);
		String url = UUID.randomUUID().toString();
		System.err.println(url);
		courseinfo.setCourseDescription(coursedeclare.getcName());
		courseinfo.setAcademicExchange("未上线");
		courseinfo.setTeachingLesson("测试教材教案");
		courseinfo.setMuitimedia("多媒体教学 视频");
		courseinfo.setTeachingMaterial(String.valueOf(u.getUserId()));
		courseinfo.setCourseNum(0);
<<<<<<< HEAD
		courseinfo.setTeachingResults(url+ofn);
		FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path, url + ofn));
		courseInfoService.insertKcXq(courseinfo);
		courseDeclareService.insertKc(coursedeclare);
		
	
=======
		courseinfo.setTeachingResults(url);
		FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path, url + ofn));
		courseInfoService.insertKcXq(courseinfo);
		
>>>>>>> e92cca4bb06cf37fd9e459ee7ced6af37b6175bf
		String url2="redirect:/admin/declare/teacherShow.do?userId="+u.getUserId();

		return url2;
	}
	
	@RequestMapping("/Xiugai")
	public @ResponseBody courseDeclare XiugaicourseInfo(int serialNumber,String cName) {
		
		courseDeclare courseDeclare =	courseDeclareService.select(serialNumber);
		courseInfo ci = courseInfoService.SelectName(courseDeclare.getcName());
		
		courseDeclare.setcName(cName);
		ci.setCourseDescription(cName);
		
		courseDeclareService.updateByPrimaryKeySelective(courseDeclare);
		courseInfoService.updateByPrimaryKeySelective(ci);
		return courseDeclare;

	}
	

	/*
	 * 老师查看自己的申请课程页
	 */
	@RequestMapping("/teacherShow.do")
	public String teacherShow(Integer userId,Model model) {

		List<courseDeclare> cd = courseDeclareService.selectUserId(userId);
		model.addAttribute("cd", cd);
		return "teacherShow";
	}
	/*
	 * 老师查看自己的申请课程页
	 */
	@RequestMapping("/ts")
	public String ts(Integer serialNumber,Model model) {
		courseDeclare cd = courseDeclareService.select(serialNumber);
		courseInfo ci = courseInfoService.SelectName(cd.getcName());
		model.addAttribute("cd", cd);
		model.addAttribute("ci", ci);
		return "gl";
	}


}
